// import { SyncPost } from '@/api/linxyun/base/index'
import { httpReqeustApi } from '/@/api/linxyun/base/index.ts';
import Lxy_params_mgr from './lxy_params_mgr.js';
import lxy_event from './lxy_page_event.js';
const httpRequest = httpReqeustApi();
// 远程调用事件;
class Lxy_page_event_remote extends lxy_event {
	_uri = null; // 远程服务URI;
	// eventConf: url, params, outParams
	// url: 请求的uri
	// params: 请求的输入参数定义
	// outParams：请求的输出参数定义
	constructor(eventConf) {
		if (!eventConf) {
			super(null);
		} else {
			super(eventConf);
			this._uri = eventConf.url || null;
		}
	}

	// expParams: 调用远程方法的参数，放url中
	// rowIndex: 如果是行操作，则为行的行号
	// 说明：调用远程方法时，两部分参数，一部分是parmas定义好的参数，放URL中，一部分是eventConf中配置好的参数，需要计算出来，放到post请求的body中
	async callEvent(expParams, rowIndex = -1) {
		if (this._uri === null || this._uri === '') {
			console.error('[Error][RemoteEvent]The uri is null.');
			return null;
		}
		console.log('[Debug][RemoteEvent]Url: ', this._uri);

		let inputParams = this.calcInputParams(rowIndex);
		inputParams = inputParams || null;

		console.log('[Debug][RemoteEvent]InputParams: ', inputParams);
		// const url = BASE_API.SERVER_IP + this._uri
		// 调用远程方法;
		if (expParams) {
			inputParams = { ...inputParams, ...expParams };
		}
		console.log('[Debug][RemoteEvent]SyncGet', this._uri, inputParams);
		let requestRes = await httpRequest.SyncGet(this._uri, inputParams);
		// console.log('[Debug][RemoteEvent]Resp:', requestRes);
		if (!requestRes) {
			console.error('[Error][RemoteEvent]Call remote method fail, uri: ', this._uri);
			return null;
		}
		console.log('[Debug][RemoteEvent]Resp:', requestRes);
		const outDataSrc = new Lxy_params_mgr(requestRes, null, null);
		if (!this.calcOutParams(outDataSrc, this._dataSrc._params)) {
			console.error('[Error][RemoteEvent]calc out params fail, uri: ', this._uri);
		}
		return requestRes;
	}
}

export default Lxy_page_event_remote;
